package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.PhoneUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import com.sinog.front.config.idempotent.Idempotent;
import com.sinog.front.config.log.Log;
import com.sinog.front.feign.biz.BjdxInfoService;
import com.sinog.front.service.user.UserFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @desc 安置帮教管理 - 帮教对象信息管理 - 全部人员管理 QbryglController
 * @author sunsf
 * @date 2020-02-07-13:51
 */
@RestController
public class QbryglController {

    @Resource
    private UserFacade userService;
    @Resource
    private BjdxInfoService bjdxInfoService;

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 全部人员管理 - 设置帮教小组 - 保存
     * @param request request
     * @param data data
     * @return Result
     */
    @Log(type = "新增", value = "新增帮教小组成员，如果有删除的，还会进行删除操作；参数中存在delIds标识删除")
    @Idempotent(paramPos = 1)
    @PostMapping("/azbj/bjdxInfo/qbrygl/saveBjxz")
    public Result saveBjxz(HttpServletRequest request,
                           @RequestParam(required = false, defaultValue = "-1") String data) {
        UserDTO user = userService.getAuthentication();
        //装数据
        data = JsonUtil.setObjectToJsonStr(data, "adduserid", user.getAccount(), "addusername", user.getDepartName());
        //取数据
        String priNum = JsonUtil.getValueFromJsonstrByKey(data, "priNum");
        String xbjfa = JsonUtil.getValueFromJsonstrByKey(data, "xbjfa");
        Boolean dataAdd = JsonUtil.getInfoFromJsonstrByKey(data, "dataAdd");
        String delIds = JsonUtil.getValueFromJsonstrByKey(data, "delIds");
        bjdxInfoService.updateByPriNum(priNum,xbjfa);
        if(dataAdd) {
            //获取新添加的小组成员
            bjdxInfoService.saveXiaozu(priNum,data,user.getAccount());
        }
        //获取删除的小组成员id
        if(!StringUtils.isBlank(delIds)) {
            //根据id删除小组成员
            List<String> lis = Arrays.asList(delIds.substring(0,delIds.length()-1).split(","));
            bjdxInfoService.delXiaozuById(lis);
        }
        return Result.success("保存成功");
    }

    /**
     * 全部人员管理--修改--获取数据
     * @param priNum priNum
     * @return Result
     */
    @RequestMapping("/azbj/bjdxInfo/qbrygl/query/getInfo")
    public Result getBaseInfo(@RequestParam(required = false, defaultValue = "-1") String priNum) {
        return bjdxInfoService.getBaseInfoByPriNum(priNum);
    }

    /**
     * 全部人员管理--获取数据基本信息
     * @param priNum priNum
     * @return Result
     */
    @RequestMapping("/azbj/bjdxInfo/qbrygl/query/getBaseInfoByPriNumMap")
    public Result getBaseInfoByPriNumMap(@RequestParam(required = false, defaultValue = "-1") String priNum) {
        Map<String,Object> prisonerMap = bjdxInfoService.getBaseInfoByPriNumMap(priNum);
        if(!CollectionUtils.isEmpty(prisonerMap)) {
            return Result.success(prisonerMap);
        }
        return Result.failure(ReturnCode.FAILURE);
    }

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 全部人员管理 - 列表
     * 安置帮教管理 - 帮教工作信息管理 - 救助管理 - 列表
     * 安置帮教管理 - 帮教工作信息管理 - 帮教管理 - 列表
     * @param pageIndex 当前页
     * @param pageSize 条数
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @param request request
     * @return String
     */
    @RequestMapping("/azbj/azbjgl/bjdxInfo/qbrygl/list")
    public String toQbryglPublicList(Integer xdFlag,Integer pageIndex,Integer pageSize,String sortField,String sortOrder,HttpServletRequest request) {
        String key = request.getParameter("querykey");
        String xdkey = request.getParameter("xd");
        String pinggu = request.getParameter("pinggu");
        UserDTO user = userService.getAuthentication();
        String departId = user.getDepartid();
        String level = user.getLevel();
        String orgid = user.getOrgid();
        return bjdxInfoService.getQbryglList(xdFlag,pageIndex,pageSize,key,xdkey,departId,level,orgid,pinggu,sortField,sortOrder);
    }

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 全部人员管理 - 设置帮教小组 - 反显工作人员列表
     * @param priNum priNum
     * @return List<Map < String,Object>>
     */
    @RequestMapping("/azbj/bjdxInfo/setBjxz/getWorkerList")
    public List<Map<String,Object>> getWorkerList(@RequestParam(required = false, defaultValue = "-1") String priNum) {
        UserDTO user = userService.getAuthentication();
        String departid = user.getDepartid();
        return bjdxInfoService.getWorkerList(priNum,departid);
    }

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 全部人员管理 - 设置帮教小组 - 反显志愿者列表
     * @param priNum priNum
     * @return List<Map < String,Object>>
     */
    @RequestMapping("/azbj/bjdxInfo/setBjxz/getZyzList")
    public List<Map<String,Object>> getZyzList(@RequestParam(required = false, defaultValue = "-1") String priNum) {
        UserDTO user = userService.getAuthentication();
        String departid = user.getDepartid();
        return bjdxInfoService.getZyzList(priNum,departid);
    }

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 全部人员管理 - 设置帮教小组 - 反显稳控民警列表
     * @param priNum priNum
     * @return List<Map < String,Object>>
     */
    @RequestMapping("/azbj/bjdxInfo/setBjxz/getWkmjList")
    public List<Map<String,Object>> getWkmjList(@RequestParam(required = false, defaultValue = "-1") String priNum) {
        UserDTO user = userService.getAuthentication();
        String departid = user.getDepartid();
        return bjdxInfoService.getWkmjList(priNum,departid);
    }

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 全部人员管理 - 设置帮教小组 - 反显家庭成员
     * @param priNum priNum
     * @return List<Map < String,Object>>
     */
    @RequestMapping("/azbj/bjdxInfo/setBjxz/getFamilyList")
    public List<Map<String,Object>> getFamilyList(@RequestParam(required = false, defaultValue = "-1") String priNum) {
        return bjdxInfoService.getFamilyList(priNum);
    }

    /**
     * 获取帮教小组列表
     * @param priNum priNum
     * @return List<Map < String,Object>>
     */
    @RequestMapping("/azbj/bjdxInfo/setBjxz/getBjxzcyList")
    public List<Map<String,Object>> getBjxzcyList(@RequestParam(required = false, defaultValue = "-1") String priNum) {
        return bjdxInfoService.getBjxzcyList(priNum);
    }

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 全部人员管理 - 设置帮教小组 - 待选人员确定
     * @param gzryids gzryids
     * @param zyzids zyzids
     * @param jtcyids jtcyids
     * @param wkmjids wkmjids
     * @return List<Map < String,Object>>
     */
    @RequestMapping("/azbj/bjdxInfo/setBjxz/getAddXzcyList")
    public List<Map<String,Object>> getAddXzcyList(
            @RequestParam(required = false, defaultValue = "-1") String gzryids,
            @RequestParam(required = false, defaultValue = "-1") String zyzids,
            @RequestParam(required = false, defaultValue = "-1") String jtcyids,
            @RequestParam(required = false, defaultValue = "-1") String wkmjids) {
        List<Map<String,Object>> list = new ArrayList<>(16);
        if(!StringUtils.equals(gzryids,Constants.NumberStr.NUM_STR_FU_ONE) && !StringUtils.equals(gzryids,"''")) {
            List<Map<String,Object>> list1 = bjdxInfoService.getWorkerListByParam(gzryids);
            transferTel(list1);
            list.addAll(list1);
        }
        if(!StringUtils.equals(zyzids,Constants.NumberStr.NUM_STR_FU_ONE) && !StringUtils.equals(zyzids,"''")) {
            List<Map<String,Object>> list1 = bjdxInfoService.getZyzListByParam(zyzids);
            transferTel(list1);
            list.addAll(list1);
        }
        if(!StringUtils.equals(jtcyids,Constants.NumberStr.NUM_STR_FU_ONE) && !StringUtils.equals(jtcyids,"''")) {
            List<Map<String,Object>> list1 = bjdxInfoService.getFamilyListByParam(jtcyids);
            transferTel(list1);
            list.addAll(list1);
        }
        if(!StringUtils.equals(wkmjids,Constants.NumberStr.NUM_STR_FU_ONE) && !StringUtils.equals(wkmjids,"''")) {
            List<Map<String,Object>> list1 = bjdxInfoService.getWkmjListByParam(wkmjids);
            transferTel(list1);
            list.addAll(list1);
        }
        return list;
    }

    /**
     * 安置帮教管理 - 档案管理 - 列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @param type 资源类型
     * @param sfsl 是否失联 1：是 2：否
     * @param inputSource 录入来源 01:司法所补录的解矫人员,05:司法所补录的监狱人员,3.看守所录入的服刑人员,4:接口进入的看守所人员,5:监狱录入的服刑人员,6:接口进入的监狱服刑人员,7:社矫录入的服刑人员,8:接口进入的社矫人员,9:监狱通过excel导入,10:社矫通过excel导入,11:看守所通过excel导入,30:湖南看守所个性化对接
     * @param querykey querykey
     * @return String
     */
    @RequestMapping("/azbj/azbjgl/bjdxInfo/archivesManager/list")
    public String toArchivesManagerList(Integer pageIndex,Integer pageSize,
                                        @RequestParam(required = false) String sortField,
                                        @RequestParam(required = false) String sortOrder,
                                        @RequestParam(required = false, defaultValue = "-1") String type,
                                        @RequestParam(required = false) String sfsl,
                                        @RequestParam(required = false, defaultValue = "-1") String inputSource,
                                        @RequestParam(required = false) String querykey) {
        UserDTO user = userService.getAuthentication();
        String departId = user.getDepartid();
        //转成int目的是把角色的0去掉，例如：03->3
        int level = Integer.parseInt(user.getLevel());
        String orgid = user.getOrgid();
        return bjdxInfoService.getArchivesManagerList(pageIndex,pageSize,sortField,sortOrder,querykey,departId,level,orgid,type,sfsl,inputSource);
    }

    /**
     * 获取户籍迁出街道信息
     * @param parentCode parentCode
     * @param priNum priNum
     * @return List<Map < String,Object>>
     */
    @RequestMapping("/azbj/bjdxInfo/getQcStreet")
    public List<Map<String,Object>> getQcStreet(
            @RequestParam(required = false, defaultValue = "-1") String parentCode,
            @RequestParam(required = false, defaultValue = "-1") String priNum) {
        return bjdxInfoService.getQcStreet(parentCode,priNum);
    }

    /**
     * 描述
     * @param list list
     */
    private void transferTel(List<Map<String,Object>> list) {
        for(Map<String,Object> stringObjectMap : list) {
            Map<String,String> map = PhoneUtil.splitTelAndPhone((String)stringObjectMap.get("tel"));
            stringObjectMap.put("fixedLine",map.get("tel"));
            stringObjectMap.put("phone",map.get("phone"));
        }
    }
}